home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / rootkits / rootkit / if.c < prev    next >
Text File  |  1994-03-01  |  9KB  |  347 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.  The Berkeley software License Agreement
  4.  * specifies the terms and conditions for redistribution.
  5.  */
  6.  
  7. #ifndef lint
  8. static    char sccsid[] = "@(#)if.c 1.1 91/11/13 SMI"; /* from UCB 5.3 4/23/86 */
  9. #endif
  10.  
  11. #include <stdio.h>
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14.  
  15. #include <net/if.h>
  16. #include <netinet/in.h>
  17. #include <netinet/in_var.h>
  18. #ifdef    ENABLE_XNS
  19. #include <netns/ns.h>
  20. #endif    ENABLE_XNS
  21.  
  22. #include <stdio.h>
  23.  
  24. extern    int aflag;
  25. extern    int tflag;
  26. extern    int nflag;
  27. extern    char *interface;
  28. extern    int unit;
  29. extern    int kread();
  30. extern    char *routename(), *netname();
  31.  
  32. /*
  33.  * Print a description of the network interfaces.
  34.  */
  35. intpr(interval, ifnetaddr)
  36.     int interval;
  37.     off_t ifnetaddr;
  38. {
  39.     struct ifnet ifnet;
  40.     union {
  41.         struct ifaddr ifa;
  42.         struct in_ifaddr in;
  43.     } ifaddr;
  44.     off_t ifaddraddr;
  45.     char name[16];
  46.  
  47.     if (ifnetaddr == 0) {
  48.         printf("ifnet: symbol not defined\n");
  49.         return;
  50.     }
  51.     if (interval) {
  52.         sidewaysintpr(interval, ifnetaddr);
  53.         return;
  54.     }
  55.     kread(ifnetaddr, &ifnetaddr, sizeof ifnetaddr);
  56.     printf("%-5.5s %-5.5s%-13.13s %-14.14s %-6.6s %-5.5s %-6.6s %-5.5s",
  57.         "Name", "Mtu", "Net/Dest", "Address", "Ipkts", "Ierrs",
  58.         "Opkts", "Oerrs");
  59.     printf(" %-6.6s", "Collis");
  60.     if (tflag)
  61.         printf(" %-6.6s", "Timer");
  62.     else
  63.         printf(" %-6.6s", "Queue");
  64.     putchar('\n');
  65.     ifaddraddr = 0;
  66.     while (ifnetaddr || ifaddraddr) {
  67.         struct sockaddr_in *sin;
  68.         register char *cp;
  69.         int n, pad;
  70.         char *index();
  71.         struct in_addr in, inet_makeaddr();
  72.         char buf[80];
  73.  
  74.         if (ifaddraddr == 0) {
  75.             /*
  76.              * No address list for the current interface:
  77.              * find the first address.
  78.              */
  79.             if (kread(ifnetaddr, &ifnet, sizeof ifnet) < 0)
  80.                 break;
  81.             if (kread((off_t)ifnet.if_name, name, 16) < 0)
  82.                 break;
  83.             name[15] = '\0';
  84.             ifnetaddr = (off_t) ifnet.if_next;
  85.             /*
  86.              * If a particular interface has been singled
  87.              * out, skip over all others.
  88.              */
  89.             if (interface) {
  90.                 if (strcmp(name, interface) != 0 ||
  91.                     unit != ifnet.if_unit)
  92.                     continue;
  93.             }
  94.             /*
  95.              * Extend device name with unit number.
  96.              */
  97.             cp = index(name, '\0');
  98.             *cp++ = ifnet.if_unit + '0';
  99.  
  100.             if ((ifnet.if_flags&IFF_UP) == 0) {
  101.                 /*
  102.                  * The interface is down: don't report on it
  103.                  * unless it's been singled out or we're
  104.                  * reporting everything.
  105.                  */
  106.                 if (!interface && !aflag)
  107.                     continue;
  108.                 *cp++ = '*';
  109.             }
  110.             *cp = '\0';
  111.  
  112.             ifaddraddr = (off_t)ifnet.if_addrlist;
  113.         }
  114.         if (ifaddraddr == 0) {
  115.             /*
  116.              * There's no address associated with the current
  117.              * interface.
  118.              */
  119.             if (!aflag) 
  120.                 continue;
  121.             printf("%-5s %-5d", name, ifnet.if_mtu);
  122.             printf("%-13.13s ", "none");
  123.             printf("%-14.14s ", "none");
  124.         } else {
  125.             printf("%-5s %-5d", name, ifnet.if_mtu);
  126.             kread(ifaddraddr, &ifaddr, sizeof ifaddr);
  127.             ifaddraddr = (off_t)ifaddr.ifa.ifa_next;
  128.             switch (ifaddr.ifa.ifa_addr.sa_family) {
  129.             case AF_UNSPEC:
  130.                 printf("%-13.13s ", "none");
  131.                 printf("%-14.14s ", "none");
  132.                 break;
  133.             case AF_INET:
  134.                 if (ifnet.if_flags & IFF_POINTOPOINT) {
  135.                     sin = (struct sockaddr_in *)
  136.                             &ifaddr.in.ia_dstaddr;
  137.                     printf("%-13s ", 
  138.                     routename(sin->sin_addr));
  139.                 } else {
  140.                     printf("%-13s ",
  141.                     netname(htonl(ifaddr.in.ia_subnet),
  142.                         ifaddr.in.ia_subnetmask));
  143.                 }
  144.                 sin = (struct sockaddr_in *)&ifaddr.in.ia_addr;
  145.                 printf("%-14s ", routename(sin->sin_addr));
  146.                 break;
  147. #ifdef ENABLE_XNS
  148.             case AF_NS: {
  149.                 struct sockaddr_ns *sns =
  150.                     (struct sockaddr_ns *)&ifaddr.in.ia_addr;
  151.                 long net;
  152.                 char host[8];
  153.  
  154.                 *(union ns_net *) &net = sns->sns_addr.x_net;
  155.                 sprintf(host, "%lxH", ntohl(net));
  156.                 upHex(host);
  157.                 printf("ns:%-8s ", host);
  158.  
  159.                 printf("%-12s ",ns_phost(sns));
  160.                 break;
  161.                 }
  162. #endif    ENABLE_XNS
  163.             default:
  164.                 pad = 29;    /* chars in this field */
  165.                 /*
  166.                  * We use this roundabout sprintf into a 
  167.                  * buffer technique here so that we
  168.                  * can count the number of characters we 
  169.                  * print.  System V printf doesn't return
  170.                  * the number of bytes written.
  171.                  */
  172.                 sprintf(buf, "af%2d: ", 
  173.                     ifaddr.ifa.ifa_addr.sa_family);
  174.                 pad -= strlen(buf);
  175.                 printf("%s", buf);
  176.  
  177.                 /*
  178.                  * Shave off trailing zero bytes in the
  179.                  * address for printing, but always print
  180.                  * at least one byte.
  181.                  */
  182.                 for (cp = (char *)&ifaddr.ifa.ifa_addr +
  183.                     sizeof(struct sockaddr) - 1;
  184.                     cp > ifaddr.ifa.ifa_addr.sa_data; --cp)
  185.                     if (*cp != 0)
  186.                         break;
  187.                 n = cp - (char *)ifaddr.ifa.ifa_addr.sa_data 
  188.                     + 1;
  189.                 cp = (char *)ifaddr.ifa.ifa_addr.sa_data;
  190.                 if (n <= 6)
  191.                     while (--n) {
  192.                         sprintf(buf, "%02d.",
  193.                                   *cp++ & 0xff);
  194.                         pad -= strlen(buf);
  195.                         printf("%s", buf);
  196.                     }
  197.                 else
  198.                     while (--n) {
  199.                         sprintf(buf, "%02d", 
  200.                                   *cp++ & 0xff);
  201.                         pad -= strlen(buf);
  202.                         printf("%s", buf);
  203.                     }
  204.                 sprintf(buf, "%02d ", *cp & 0xff);
  205.                 pad -= strlen(buf);
  206.                 printf("%s", buf);
  207.                 if (pad > 0)
  208.                     while (pad--)
  209.                         printf(" ");
  210.                 break;
  211.             }
  212.         }
  213.         printf("%-7d %-4d %-7d %-4d %-6d",
  214.             ifnet.if_ipackets, ifnet.if_ierrors,
  215.             ifnet.if_opackets, ifnet.if_oerrors,
  216.             ifnet.if_collisions);
  217.         if (tflag)
  218.             printf(" %-6d", ifnet.if_timer);
  219.         else
  220.             printf(" %-6d", ifnet.if_snd.ifq_len);
  221.         putchar('\n');
  222.     }
  223. }
  224.  
  225. #define    MAXIF    20
  226. struct    iftot {
  227.     char    ift_name[16];        /* interface name */
  228.     int    ift_ip;            /* input packets */
  229.     int    ift_ie;            /* input errors */
  230.     int    ift_op;            /* output packets */
  231.     int    ift_oe;            /* output errors */
  232.     int    ift_co;            /* collisions */
  233. } iftot[MAXIF];
  234.  
  235. /*
  236.  * Print a running summary of interface statistics.
  237.  * Repeat display every interval seconds, showing
  238.  * statistics collected over that interval.  First
  239.  * line printed at top of screen is always cumulative.
  240.  */
  241. sidewaysintpr(interval, off)
  242.     int interval;
  243.     off_t off;
  244. {
  245.     struct ifnet ifnet;
  246.     off_t firstifnet;
  247.     static char sobuf[BUFSIZ];
  248.     register struct iftot *ip, *total;
  249.     register int line;
  250.     struct iftot *lastif, *sum, *interesting;
  251.     int maxtraffic, traffic;
  252.  
  253.     setbuf(stdout, sobuf);
  254.     kread(off, &firstifnet, sizeof (off_t));
  255.     lastif = iftot;
  256.     sum = iftot + MAXIF - 1;
  257.     total = sum - 1;
  258.     maxtraffic = 0, interesting = iftot;
  259.     for (off = firstifnet, ip = iftot; off;) {
  260.         char *cp;
  261.  
  262.         kread(off, &ifnet, sizeof ifnet);
  263.         traffic = ifnet.if_ipackets + ifnet.if_opackets;
  264.         if (traffic > maxtraffic)
  265.             maxtraffic = traffic, interesting = ip;
  266.         ip->ift_name[0] = '(';
  267.         kread((int)ifnet.if_name, ip->ift_name + 1, 15);
  268.         if (interface && strcmp(ip->ift_name + 1, interface) == 0 &&
  269.             unit == ifnet.if_unit) {
  270.             interesting = ip;
  271.             maxtraffic = 0x7FFFFFFF;
  272.         }
  273.         ip->ift_name[15] = '\0';
  274.         cp = index(ip->ift_name, '\0');
  275.         sprintf(cp, "%d)", ifnet.if_unit);
  276.         ip++;
  277.         if (ip >= iftot + MAXIF - 2)
  278.             break;
  279.         off = (off_t) ifnet.if_next;
  280.     }
  281.     lastif = ip;
  282. banner:
  283.     printf("    input   %-6.6s    output        ", interesting->ift_name);
  284.     if (lastif - iftot > 0)
  285.         printf("   input  (Total)    output       ");
  286.     for (ip = iftot; ip < iftot + MAXIF; ip++) {
  287.         ip->ift_ip = 0;
  288.         ip->ift_ie = 0;
  289.         ip->ift_op = 0;
  290.         ip->ift_oe = 0;
  291.         ip->ift_co = 0;
  292.     }
  293.     putchar('\n');
  294.     printf("%-7.7s %-5.5s %-7.7s %-5.5s %-6.6s ",
  295.         "packets", "errs", "packets", "errs", "colls");
  296.     if (lastif - iftot > 0)
  297.         printf("%-7.7s %-5.5s %-7.7s %-5.5s %-6.6s ",
  298.             "packets", "errs", "packets", "errs", "colls");
  299.     putchar('\n');
  300.     fflush(stdout);
  301.     line = 0;
  302. loop:
  303.     sum->ift_ip = 0;
  304.     sum->ift_ie = 0;
  305.     sum->ift_op = 0;
  306.     sum->ift_oe = 0;
  307.     sum->ift_co = 0;
  308.     for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
  309.         kread(off, &ifnet, sizeof ifnet);
  310.         if (ip == interesting)
  311.             printf("%-7d %-5d %-7d %-5d %-6d ",
  312.                 ifnet.if_ipackets - ip->ift_ip,
  313.                 ifnet.if_ierrors - ip->ift_ie,
  314.                 ifnet.if_opackets - ip->ift_op,
  315.                 ifnet.if_oerrors - ip->ift_oe,
  316.                 ifnet.if_collisions - ip->ift_co);
  317.         ip->ift_ip = ifnet.if_ipackets;
  318.         ip->ift_ie = ifnet.if_ierrors;
  319.         ip->ift_op = ifnet.if_opackets;
  320.         ip->ift_oe = ifnet.if_oerrors;
  321.         ip->ift_co = ifnet.if_collisions;
  322.         sum->ift_ip += ip->ift_ip;
  323.         sum->ift_ie += ip->ift_ie;
  324.         sum->ift_op += ip->ift_op;
  325.         sum->ift_oe += ip->ift_oe;
  326.         sum->ift_co += ip->ift_co;
  327.         off = (off_t) ifnet.if_next;
  328.     }
  329.     if (lastif - iftot > 0)
  330.         printf("%-7d %-5d %-7d %-5d %-6d\n",
  331.             sum->ift_ip - total->ift_ip,
  332.             sum->ift_ie - total->ift_ie,
  333.             sum->ift_op - total->ift_op,
  334.             sum->ift_oe - total->ift_oe,
  335.             sum->ift_co - total->ift_co);
  336.     *total = *sum;
  337.     fflush(stdout);
  338.     line++;
  339.     if (interval)
  340.         sleep(interval);
  341.     if (line == 21)
  342.         goto banner;
  343.     goto loop;
  344.     /*NOTREACHED*/
  345. }
  346.  
  347.